package com.thiswhyme.common.response;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * Created by thiswhyme on 2019/11/18.
 */
@Data
@ApiModel("标准返回参数结构")
public class ResponseVo<T> {

    @ApiModelProperty("成功标识")
    private boolean success;

    @ApiModelProperty("响应码")
    private String code;

    @ApiModelProperty("业务数据")
    private T data;

    @ApiModelProperty("提示信息")
    private String msg;

    @ApiModelProperty("附加信息")
    private String extraMsg;

    public static <T> ResponseVo<T> success(T data, String msg, String extraMsg) {
        ResponseVo<T> response = new ResponseVo<>();
        response.setSuccess(true);
        response.setCode(ResponseCode.SUCCESS.getCode());
        response.setData(data);
        response.setMsg(msg);
        response.setExtraMsg(extraMsg);
        return response;
    }

    public static <T> ResponseVo<T> success(T data, String msg) {
        return success(data, msg, "");
    }

    public static <T> ResponseVo<T> success(T data) {
        return success(data, ResponseCode.SUCCESS.getMsg());
    }

    public static <T> ResponseVo<T> success() {
        return success(null);
    }

    public static <T> ResponseVo<T> failure(String code, String msg, String extraMsg, T data) {
        ResponseVo<T> response = new ResponseVo<>();
        response.setSuccess(false);
        response.setCode(code);
        response.setData(data);
        response.setMsg(msg);
        response.setExtraMsg(extraMsg);
        return response;
    }

    public static <T> ResponseVo<T> failure(String code, String msg, String extraMsg) {
        return failure(code, msg, extraMsg, null);
    }

    public static <T> ResponseVo<T> failure(String code, String msg) {
        return failure(code, msg, null);
    }

    public static <T> ResponseVo<T> failure() {
        return failure(ResponseCode.SYSTEM_EXCEPTION.getCode(), ResponseCode.SYSTEM_EXCEPTION.getMsg());
    }
}
